package com.rrjjg.app.admin.api;

import com.netflix.discovery.converters.Auto;
import com.rrjjg.app.admin.api.vo.PropertiesHistoryVo;
import com.rrjjg.app.admin.api.vo.PropertiesVo;
import com.rrjjg.app.admin.api.vo.PushVo;
import com.rrjjg.app.admin.bus.BPropertiesService;
import com.rrjjg.app.admin.data.storage.entity.Properties;
import com.rrjjg.app.admin.module.enums.ErrorCode;
import com.rrjjg.app.core.beans.ResultData;
import com.rrjjg.app.core.exception.MsgEnum;
import com.rrjjg.app.core.exception.RjException;
import com.rrjjg.app.core.utils.StringUtil;
import com.rrjjg.app.core.utils.ValidationUtil;
import com.rrjjg.app.mybatis.bean.PageData;
import com.rrjjg.app.mybatis.bean.PageVo;
import com.rrjjg.app.mybatis.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.rrjjg.app.admin.data.storage.entity.PropertiesHistory;

import java.util.List;

/**
 * <p>
 * 配置表 前端控制器
 * </p>
 *
 * @author vayne
 * @since 2023-04-06
 */
@RestController
@RequestMapping("/rrjjg/properties")
public class PropertiesController {
    @Autowired
    BPropertiesService bPropertiesService;


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultData<Boolean> add(@RequestBody PropertiesVo vo) {
        ValidationUtil.validation(vo);
        boolean save = bPropertiesService.getiPropertiesService().save(PageUtil.transformation(vo, Properties::new));
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResultData<Boolean> del(@RequestBody PropertiesVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.del.class);
        boolean save = bPropertiesService.getiPropertiesService().removeById(vo.getId());
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultData<Boolean> update(@RequestBody PropertiesVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.update.class);
        boolean save = bPropertiesService.getiPropertiesService().updateById(PageUtil.transformation(vo,Properties::new));
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/upOrDown", method = RequestMethod.POST)
    public ResultData<Void> upOrDown(@RequestBody PushVo vo) {
        ValidationUtil.validation(vo,ValidationUtil.update.class);
        bPropertiesService.upOrDown(vo.getApplication(), vo.getType(),vo.getAdds());
        return new ResultData<>();
    }


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultData<PageData<Properties>> list(@RequestBody PageVo<PropertiesVo> vo) {
        PropertiesVo data = vo.getData();
        PageData<Properties> propertiesPageData = bPropertiesService.queryPage(PageUtil.transformation(data, Properties::new), vo.getPage(), vo.getSize());
        return new ResultData<>(propertiesPageData);
    }

    //http://localhost:8081/actuator/mappings
    // eureka api
    //http://192.168.56.1:8080/eureka/apps/CONFIG-ADMIN/config-admin:true:8084/status?value=OUT_OF_SERVICE
    @RequestMapping(value = "/push", method = RequestMethod.POST)
    public ResultData<List<String>> push(@RequestBody PushVo vo) {
        ValidationUtil.validation(vo,ValidationUtil.add.class);
        List<String> push = bPropertiesService.push(vo.getApplication(), vo.getAdds());
        return new ResultData<>(push);
    }


    @RequestMapping(value = "/addh", method = RequestMethod.POST)
    public ResultData<Boolean> addh(@RequestBody PropertiesHistoryVo vo) {
        ValidationUtil.validation(vo);
        boolean save = bPropertiesService.getiPropertiesHistoryService().save(PageUtil.transformation(vo, PropertiesHistory::new));
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/delh", method = RequestMethod.POST)
    public ResultData<Boolean> delh(@RequestBody PropertiesHistoryVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.del.class);
        boolean save = bPropertiesService.getiPropertiesHistoryService().removeById(vo.getId());
        return new ResultData<>(save);
    }

    @RequestMapping(value = "/updateh", method = RequestMethod.POST)
    public ResultData<Boolean> updateh(@RequestBody PropertiesHistoryVo vo) {
        ValidationUtil.validation(vo, ValidationUtil.update.class);
        boolean save = bPropertiesService.getiPropertiesHistoryService().updateById(PageUtil.transformation(vo,PropertiesHistory::new));
        return new ResultData<>(save);
    }


    @RequestMapping(value = "/listh", method = RequestMethod.POST)
    public ResultData<PageData<Properties>> listh(@RequestBody PageVo<PropertiesHistoryVo> vo) {
        PropertiesHistoryVo data = vo.getData();
        PageData<Properties> propertiesPageData = bPropertiesService.queryPageh(PageUtil.transformation(data, PropertiesHistory::new), vo.getPage(), vo.getSize());
        return new ResultData<>(propertiesPageData);
    }


    @RequestMapping(value = "/eurekas", method = RequestMethod.POST)
    public ResultData<List<String>> eurekas(@RequestBody PropertiesHistoryVo vo) {
        if(StringUtils.isEmpty(vo.getApplication())){
            throw new RjException(MsgEnum.PARAM_ERR);
        }
        List<String> strings = bPropertiesService.eurekaSerive(vo.getApplication());
        return new ResultData<>(strings);
    }

}
